uniform vec4 lightPos;
uniform mat4 camera;
uniform mat4 cube;
varying vec3 eyeVec;
varying vec3 lightVec;
varying vec3 normal;
varying vec3 position;
varying vec4 color;

void main()
{
	gl_Position = ftransform();
	gl_TexCoord[0] = gl_MultiTexCoord0;

	normal = gl_Normal;
	vec3 tangent;
	vec3 binormal;

	vec3 c1 = cross(normal, vec3(0.0, 0.0, 1.0));
	vec3 c2 = cross(normal, vec3(0.0, -1.0, 0.0));

	if(length(c1)>length(c2))
	{
		tangent = c1;
	}
	else
	{
		tangent = c2;
	}

	tangent = normalize(tangent);

	binormal = cross(normal, tangent);
	binormal = normalize(binormal);

	mat3 TBNMatrix = mat3(tangent, binormal, normal);

	vec4 pos = gl_ModelViewMatrix * gl_Vertex;
	position = pos.xyz / pos.w;
	vec3 tmpVec = vec3(normalize((gl_ModelViewProjectionMatrix * gl_LightSource[0].position).xyz - position));
	lightVec.x = dot(tmpVec, tangent);
	lightVec.y = dot(tmpVec, binormal);
	lightVec.z = dot(tmpVec, normal);

	eyeVec.x = dot(-position, tangent);
	eyeVec.y = dot(-position, binormal);
	eyeVec.z = dot(-position, normal);

	color = gl_Color;
}
